/*
  聊天中根据时间显示列表
 */
export const gruopByTime = (groupObj = [], arr, type = 0) => {
  arr.forEach((item, index) => {
    if (item.createtime) {
      if (groupObj.length <= 0) {
        const obj = {
          time: item.createtime,
          data: [item]
        }
        groupObj.push(obj)
      } else {
        if (type === 1) {
          if (diff5min(item.createtime, groupObj[0].time, type)) {
            const obj = {
              time: item.createtime,
              data: [item]
            }
            groupObj.unshift(obj)
          } else {
            groupObj[0].data.unshift(item)
          }
        } else {
          if (diff5min(item.createtime, groupObj[groupObj.length - 1].time)) {
            const obj = {
              time: item.createtime,
              data: [item]
            }
            groupObj.push(obj)
          } else {
            groupObj[groupObj.length - 1].data.push(item)
          }
        }
      }
    }
  })
  return groupObj
}

function diff5min (start, end, type = 0) {
  if (!end) {
    return true
  }

  if (start && end) {
    start = start.replace(/-/g, '/')
    end = end.replace(/-/g, '/')
    const startTime = new Date(start)
    const endTime = new Date(end)

    let diff = startTime.getTime() - endTime.getTime()
    if (type === 1) {
      diff = endTime.getTime() - startTime.getTime()
    }

    if (diff > 60 * 1000 * 5) {
      return true
    } else {
      return false
    }
  }
}
